using UnityEngine;
using System.Collections;

public class ShardController : MonoBehaviour {
	private float BornTime;
	private float LifeTime;
	private Color BaseColor;
	private bool FirstUpdate;
	
	// Use this for initialization
	void Awake () {
		BornTime = Time.timeSinceLevelLoad;
		LifeTime = Random.Range( 2.0f, 4.0f );
		FirstUpdate = true;
	}
	
	// Update is called once per frame
	void Update () {
		if( FirstUpdate )
		{
			BaseColor = renderer.material.color;
			FirstUpdate = false;
		}
		
		float lifeleft =  LifeTime - ( Time.timeSinceLevelLoad - BornTime );
		
		float alpha = lifeleft / LifeTime;
		Color col;
		col.r = BaseColor.r;
		col.g = BaseColor.g;
		col.b = BaseColor.b;
		col.a = alpha;
		renderer.material.color = col;
	
		if( lifeleft <= 0.0f )
		{
			Destroy( gameObject );
		}
		
	}
}
